/*  Authenticater, version 0.1.0
 *  (c) 2007 Sven Hecht
 *
 *  Authenticater.js is freely distributable under the terms of an MIT-style license.
 *  For details, see http://sven.hecht.googlepages.com/index.html
 *
/*--------------------------------------------------------------------------*/

var Authenticater = Class.create();
Authenticater.prototype = {
  initialize: function(_authenticatedCallback, _logoffCallback)
  {
  	this.m_AuthenticatedCallback = ( typeof _authenticatedCallback == 'undefined' ? function(){} : _authenticatedCallback );
  	this.m_LogoffCallback = ( typeof _logoffCallback == 'undefined' ? function(){} : _logoffCallback );
  	
  	this.m_Authenticated = false;
  	this.m_AuthenticatedUsername = '';
  	this.m_AuthenticatedUserData = '';
  	this.m_AuthenticatedUserPath = '';
  },
  
  Authenticate: function(_username, _password)
  {
		var result = false;
		
  	if (typeof MD5 == 'undefined') {
  	
  		$import('md5.js');
  		window.setTimeout(this.Authenticate.bind(this, _username, _password), 10);
  		
  	} else {
  	
			var hashedUsername = MD5(_username);
			_password = MD5(_password);
			
			this.m_AuthenticatedUsername = _username
			this.m_AuthenticatedUserPath = hashedUsername + '/' + _password + '/userdata.txt'
			var ajax = new Ajax.Request(this.m_AuthenticatedUserPath, {
						method: 'get',
						onSuccess: this.Authenticate_AjaxSuccessCallback.bind(this),
						onFailure: this.Authenticate_AjaxFailureCallback.bind(this),
						onException: this.Authenticate_AjaxFailureCallback.bind(this)
					});
		}
 	},
 	
 	Authenticate_AjaxSuccessCallback: function(transport)
 	{
		if (transport.responseText) {
			var evaledData = eval(transport.responseText);
			this.m_AuthenticatedUserData = ( evaledData != null && evaledData != undefined ? evaledData : transport.responseText );
			this.m_AuthenticatedCallback(true);
		}
 	},
 	
 	Authenticate_AjaxFailureCallback: function(transport)
 	{
		this.m_AuthenticatedCallback(false);
		this.ClearData();
 	},
 	
 	Logoff: function()
 	{
 		this.m_LogoffCallback();
 		this.ClearData();
 	}, 
 	
 	ClearData: function()
 	{
  	this.m_Authenticated = false;
  	this.m_AuthenticatedUsername = '';
  	this.m_AuthenticatedUserData = '';
  	this.m_AuthenticatedUserPath = '';
 	},
 	
 	GetPrivateData: function()
 	{
 	}
 }